package base;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import dao.ClassinformationDao;
import dao.CourseDao;
import dao.DepartmentDao;
import dao.MajorDao;
import dao.ManagerDao;
import dao.StudentDao;
import model.Classinformation;
import model.Course;
import model.Department;
import model.Major;
import model.Manager;
import model.Student;

/**
 * 学院编号、系编号、班级编号、教师、学生编号、课程编号 自动生成
 * 
 * @author Su
 *
 */
public class NumberGeneration {

	/**
	 * 学院编号 由两位十进制数组成 00~99 由 01 开始 示例 01 信息工程学院
	 * 
	 * @return
	 */
	public static String createDepartmentNum() {
		int departmentNum;
		ArrayList<Department> arrayList = DepartmentDao.select();
		if (arrayList.isEmpty()) {
			return "01";
		}
		Department department = arrayList.get(arrayList.size() - 1);
		departmentNum = Integer.parseInt(department.getD_id()) + 1;
		if (departmentNum < 10) {
			return "0" + departmentNum;
		}
		return "" + departmentNum;
	}

	/**
	 * 系编号 由学院编号 + 两位十进制数组成 00~99 由 01 开始 示例 0101 信息工程学院计算机科学与技术系
	 * 
	 * @return
	 */
	public static String createMajorNum(Department department) {
		int majorNum;
		ArrayList<Major> arrayList = MajorDao.select();
		ArrayList<Major> arrayListMajors = new ArrayList<Major>();
		for (Major major : arrayList) {
			if (major.getD_id().equals(department.getD_id())) {
				arrayListMajors.add(major);
			}
		}
		if (arrayListMajors.isEmpty()) {
			return department.getD_id() + "01";
		}
		Major major = arrayListMajors.get(arrayListMajors.size() - 1);
		majorNum = Integer.parseInt(major.getM_id()) + 1;
		if (majorNum < 1000) {
			return "0" + majorNum;
		}
		return "" + majorNum;
	}

	/**
	 * 班级编号 由大写数字 + 年份后两位 + 两位十进制数组成 00~99 由 01 开始 示例 B 0101 1705
	 * 
	 * @return
	 */
	public static String createClassNum(Major major, Integer year) {
		int classNum;
		String string;
		ArrayList<Classinformation> arrayList = ClassinformationDao.select();
		ArrayList<Classinformation> arrayListClass = new ArrayList<Classinformation>();
		for (Classinformation classinformation : arrayList) {
			if (major.getM_id().equals(classinformation.getM_id())) {
				arrayListClass.add(classinformation);
			}
		}
		if (arrayListClass.isEmpty()) {
			return "B" + major.getM_id() + year + "01";
		}

		Classinformation classinformation = arrayListClass.get(arrayListClass.size() - 1);
		char[] array = classinformation.getCla_id().toCharArray();
		StringBuffer sb = new StringBuffer();
		for (int i = 7; i < array.length; i++) {
			sb.append(array[i]);
		}
		classNum = Integer.parseInt(sb.toString()) + 1;
		string = "" + classNum;
		if (classNum < 10) {
			string = "0" + string;
		}
		string = year + string;
		char[] c = string.toCharArray();
		for (int i = 0; i < c.length; i++) {
			array[i + 5] = c[i];
		}

		return String.valueOf(array);
	}
	/**
	 * 班级编号 由大写数字 + 年份后两位 + 两位十进制数组成 00~99 由 01 开始 示例 B 0101 1705
	 * 
	 * @return
	 */
	public static String createClassName(Major major, Integer year) {
		String string = createClassNum(major, year);
		char[] array = string.toCharArray();
		StringBuffer sb = new StringBuffer();
		for (int i = 5; i < array.length; i++) {
			sb.append(array[i]);
		}
		return "B" + sb.toString();
	}

	/**
	 * 教师编号 由学院编号 + 系编号 + 四位十进制数组成 0000~9999 由 0001 开始 示例 T0101 0001 T0101 0001
	 * T01010201
	 * 
	 * @return
	 */
	public static String createTeacherNum(Major major) {
		int teacherNum;
		String string;
		ArrayList<Manager> arrayList = ManagerDao.select();
		ArrayList<Manager> arrayListCourse = new ArrayList<Manager>();
		for (Manager course : arrayList) {
			if (major.getM_id().equals(course.getM_id())) {
				arrayListCourse.add(course);
			}
		}
		if (arrayListCourse.isEmpty()) {
			return "T" + major.getM_id() + "0001";
		}

		Manager manager = arrayListCourse.get(arrayListCourse.size() - 1);
		char[] array = manager.getT_id().toCharArray();
		StringBuffer sb = new StringBuffer();
		for (int i = 5; i < array.length; i++) {
			sb.append(array[i]);
		}
		teacherNum = Integer.parseInt(sb.toString()) + 1;
		string = "" + teacherNum;
		if (teacherNum < 1000) {
			string = "0" + string;
		}
		if (teacherNum < 100) {
			string = "0" + string;
		}
		if (teacherNum < 10) {
			string = "0" + string;
		}
		char[] c = string.toCharArray();
		for (int i = 0; i < c.length; i++) {
			System.out.print(c[i]);
			array[i + 5] = c[i];
		}

		return String.valueOf(array);
	}

	/**
	 * 学生编号 由学院编号 + 系编号 + 三位十进制数组成 000~999 由 001 开始 示例 S 0101 001
	 * 
	 * @return
	 */
	public static String createStudentNum(Major major) {
		int studentNum;
		String string;
		ArrayList<Student> arrayList = StudentDao.select();
		ArrayList<Student> arrayListCourse = new ArrayList<Student>();
		for (Student course : arrayList) {
			if (major.getM_id().equals(course.getM_id())) {
				arrayListCourse.add(course);
			}
		}
		if (arrayListCourse.isEmpty()) {
			return "S" + major.getM_id() + "001";
		}

		Student manager = arrayListCourse.get(arrayListCourse.size() - 1);
		char[] array = manager.getStu_id().toCharArray();
		StringBuffer sb = new StringBuffer();
		for (int i = 5; i < array.length; i++) {
			sb.append(array[i]);
		}
		studentNum = Integer.parseInt(sb.toString()) + 1;
		string = "" + studentNum;
		if (studentNum < 100) {
			string = "0" + string;
		}
		if (studentNum < 10) {
			string = "0" + string;
		}
		char[] c = string.toCharArray();
		for (int i = 0; i < c.length; i++) {
			array[i + 5] = c[i];
		}

		return String.valueOf(array);
	}

	/**
	 * 课程编号 由 C + 学院编号 + 系编号 + 课程流水号（在本课程类别下编流水号）二位十进制数组成 00~99 由 01 开始 示例C 01 01 1
	 * 01
	 * 
	 * @return
	 */
	public static String createCourseNum(Major major) {
		int courseNum;
		String string;
		ArrayList<Course> arrayList = CourseDao.select();
		ArrayList<Course> arrayListCourse = new ArrayList<Course>();
		for (Course course : arrayList) {
			if (major.getM_id().equals(course.getM_id())) {
				arrayListCourse.add(course);
			}
		}
		if (arrayListCourse.isEmpty()) {
			return "C" + major.getM_id() + "01";
		}

		Course course = arrayListCourse.get(arrayListCourse.size() - 1);
		char[] array = course.getCou_id().toCharArray();

		String couId = array[5] + "" + array[6];
		courseNum = Integer.parseInt(couId) + 1;
		string = "" + courseNum;
		if (courseNum < 10) {
			string = "0" + courseNum;
		}
		char[] c = string.toCharArray();
		array[5] = c[0];
		array[6] = c[1];

		return String.valueOf(array);
	}

	public static int getDate() {
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR) - 2000;
		return year;
	}

	public static void main(String[] args) {
		String string = "B12341234";
		char[] array = string.toCharArray();
		StringBuffer sb = new StringBuffer();
		for (int i = 5; i < array.length; i++) {
			sb.append(array[i]);
		}
		System.out.println("B" + sb.toString());
	}
}
